<?php
require_once 'data_form'.EXT;

class Current_holding extends Data_form{
    
    function __construct()
    {
        parent::__construct();
        
    }
    
    function index()
    {
        $this->load->model('current_holding_model');
        $data = array();
        $submit    =    ($this->input->post("submit") == "0" ? false : true);
        //load du lieu o trong db ra truoc
        $row    =    $this->current_holding_model->get_by('customer_id',$_SESSION["customer"]["id"]);
        
        if($_SERVER['REQUEST_METHOD'] == 'GET' && $row)
        {
            //parse from row to array
            $data    =    array(
                "id" => $row->id,
                "customer_id" => $row->customer_id,
                "SB" => $row->SB,
                "SBR" => $row->SBR,
                "SS" => $row->SS,
                "SSR" => $row->SSR,
                "US" => $row->US,
                "USR" => $row->USR,
                "B" => $row->B,
                "BR" => $row->BR,
                "FD" => $row->FD,
                "FDR" => $row->FDR,
                "IP" => $row->IP,
                "IPR" => $row->IPR,
                "CPF" => $row->CPF,
                "CPFR" => $row->CPFR,
                "explanation" => $row->explanation
            );
            
            $_SESSION["data"] = $data;
        }
        else if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
        	$data["customer_id"]  =    $_SESSION["customer"]["id"];
                $data["SB"]           =    $_POST["textfield5"];
                $data["SBR"]          =    $_POST["textfield8"];
                $data["SS"]           =    $_POST["textfield2"];
                $data["SSR"]          =    $_POST["textfield9"];
                $data["US"]           =    $_POST["textfield6"];
                $data["USR"]          =    $_POST["textfield10"];
                $data["B"]            =    $_POST["textfield7"];
                $data["BR"]           =    $_POST["textfield11"];
                $data["FD"]           =    $_POST["textfield"];
                $data["FDR"]          =    $_POST["textfield12"];
                $data["IP"]           =    $_POST["textfield3"];
                $data["IPR"]          =    $_POST["textfield13"];
                $data["CPF"]          =    $_POST["textfield4"];
                $data["CPFR"]         =    $_POST["textfield14"];
            if (isset($_POST['btnCalculate']) && $_POST['btnCalculate'] == 'Calculate')
            {
                //$data = isset($_SESSION["data"]);
               //lay data tren form post ve
                
                if($row){
                	$data['explanation']	=	$row->explanation;
                }
                
                $_SESSION["existing_coverage_data"] = $data; 
            }
            else
            {
                 //$data = $_SESSION["existing_coverage_data"];
                 $data["explanation"] = $_POST["Explanation"];
                 //neu du lieu da co trong db
                if($row){
                    //update du lieu dang co trong db
                    $this->current_holding_model->update($row->id,$data);
                }
                else
                {
                    //tao moi du lieu trong db
                    $this->current_holding_model->insert($data);
                }
                
                redirect(current_link());
            }       
        }
        
        //gan du lieu o trong row vao 1 bien trong data
        //truyen ra view thong qua data['row'] 
        $this->data['row']    =    $data;  
        $this->load->view("current_holding/index", $this->data);  
    }
}
?>
